package com.oa.dao.impl;

import com.oa.dao.JdbcDao;
import com.oa.domain.AppsData;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.Map;

@Repository
public class JdbcDaoImpl implements JdbcDao {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    private static final Logger logger= LoggerFactory.getLogger(JdbcDaoImpl.class);


    @Override
    public Long update(AppsData data) {
        String sql="insert oa_apps_"+data.getTplid()+"(appid,tplid,tpltype,date) values(?,?,?,?)";
        int id=jdbcTemplate.update(sql,data.getAppid(),data.getTplid(),data.getTypetype(),System.currentTimeMillis());
        return (long) id;
    }

    @Override
    public void update(Long typeId, Map<String, Object> map,Long appsId) {
        StringBuilder builder= new StringBuilder();
        builder.append("update oa_apps_"+typeId+" set ");
        int num=0;
        for (Map.Entry<String,Object> entry : map.entrySet()){
            if (num>0){
                builder.append(" ,");
            }
            num++;
            builder.append(entry.getKey()+"='"+entry.getValue()+"'");
        }
        builder.append("where appid='"+appsId+"' and tplId='"+typeId+"';");
        String sql= builder.toString();
        logger.warn("更新表格是sql语句为:"+sql);
        jdbcTemplate.execute(sql);
    }

    @Override
    public int query(Long typeId, Long appsId, String name) {
        String sql="select "+name+" from oa_apps_"+typeId+" where appid='"+appsId+"' and tplid='"+typeId+"'";
        int value= jdbcTemplate.queryForObject(sql,Integer.class);
        return value;
    }

    @Override
    public String queryString(Long typeId, Long appsId, String name) {
        String sql="select "+name+" from oa_apps_"+typeId+" where appid='"+appsId+"' and tplid='"+typeId+"'";
        String value= jdbcTemplate.queryForObject(sql,String.class);
        logger.warn("属性:"+name+",值:"+value);
        return value;
    }

    @Override
    public Map<String, Object> queryObject(Long typeId, Long appsId) {
        String sql="select * from oa_apps_"+typeId+" where appid='"+appsId+"' and tplid='"+typeId+"'";
        Map<String,Object> map=jdbcTemplate.queryForMap(sql);
        logger.warn("查询的结果:"+map);
        return map;
    }
}
